version 16.1
clear all
cd "MYPATH\derived\make_master_dataset"
adopath + ../../ado/
cap log close
log using admin_prep.log, replace

preliminaries
foreach PATH in RESULTS TEMP {
	cap mkdir "$TEMP\make_master_dataset"
    if "`PATH'" != "TEMP" cap mkdir "${`PATH'}\derived\make_master_dataset\figures"
}

graph set window fontface default
graph set ps fontface default
graph set window fontfacemono "Consolas"
graph set ps fontfacemono "Consolas"


program main 
    merge_admin_data
end

program merge_admin_data
	
	use "MYPATH\MYPATH.dta", clear
	rename lopnr parent_lopnr
	rename barn_lopnr lopnr
	unique lopnr
	merge m:1 lopnr using "MYPATH\MYPATH.dta", ///
	  assert(1 2 3) keep(1 2 3) keepusing(fodelsear fodelsemanad)
	tab _merge
	keep if _merge == 3
	destring fodelsear, replace
	destring fodelsemanad, replace
	drop _merge
	drop if fodelsear < 1995
	unique lopnr
	  
	duplicates tag parent_lopnr fodelsear fodelsemanad, gen(dup)
	tab dup 
	sort parent_lopnr fodelsear fodelsemanad
	drop if dup > 0
	rename fodelsear birth_yr_admin
	rename fodelsemanad birth_mo_admin
	rename lopnr childid
	rename parent_lopnr lopnr
	drop aterpnr adop dup 
	save "$TEMP\derived\make_master_dataset\childrenlist.dta", replace
end



* Execute
main
